<?php
session_start();
ini_set('max_execution_time', 100000);
$carisesi= $_SESSION['carisesimct'] ;
$host = "localhost";
$user = "root";
$pass = "";
$db ="eeg";
$koneksi = mysql_connect($host, $user, $pass);
if (!$koneksi) {
echo "Koneksi ke server tidak berhasil";
};
$database = mysql_select_db($db);
if (!$database) {
echo "Koneksi ke database tidak berhasil";
}
mysql_select_db($db) or die ("Database not Found !");


//fungsi mean
function average($arr)
{
   if (!is_array($arr)) return false;

   return array_sum($arr)/count($arr);
}


//fungsi sum
function sum($arr)
{
   if (!is_array($arr)) return false;

   return array_sum($arr);
}





//mencari mean 60 detik x1

$sql = "SELECT COUNT(sesi) FROM median1d where sesi='$carisesi'"; 
$rs_result = mysql_query($sql); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0]; 
$i=0;


$batas=60;
$menu = array();
$alpha = array();
$beta = array();
$lowalpha = array();
$highalpha = array();
$lowbeta = array();
$highbeta = array();
$sql = "SELECT * FROM median1d where sesi='$carisesi' LIMIT $i,$batas"; 
$rs_result = mysql_query ($sql); 

while( $rows = mysql_fetch_row($rs_result) ){
    

			 $menu[] = $rows[2];
			 $alpha[] = $rows[3];
			 $beta[] = $rows[4];
			 $lowalpha[] = $rows[5];
			 $highalpha[] = $rows[6];
			 $lowbeta[] = $rows[7];
			 $highbeta[] = $rows[8];
			
				}



//sum 60 detik cari varian y1

$sql = "SELECT COUNT(sesi) FROM median1d where sesi='$carisesi'"; 
$rs_result = mysql_query($sql); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0]; 
$i2=0;


$batas=60;
$menu2 = array();
$alpha2 = array();
$beta2 = array();
$lowalpha2 = array();
$highalpha2 = array();
$lowbeta2 = array();
$highbeta2 = array();

$sql = "SELECT * FROM median1d where sesi='$carisesi' LIMIT $i2,$batas"; 
$rs_result = mysql_query ($sql); 

while( $rows = mysql_fetch_row($rs_result) ){
    

			 $menu2[] = $rows[2];
			 $alpha2[] = $rows[3];
			 $beta2[] = $rows[4];
			 $lowalpha2[] = $rows[5];
			 $highalpha2[] = $rows[6];
			 $lowbeta2[] = $rows[7];
			 $highbeta2[] = $rows[8];
			
				}


$sumy= sum ($menu2);
$sumyalpha= sum ($alpha2);
$sumybeta= sum ($beta2);
$sumylowalpha= sum ($lowalpha2);
$sumyhighalpha= sum ($highalpha2 );
$sumylowbeta= sum ($lowbeta2);
$sumyhighbeta= sum ($highbeta2);

			  
			  
			 
			  
			 
			
// sum y kuadrat


$sql = "SELECT COUNT(sesi) FROM median1d where sesi='$carisesi'"; 
$rs_result = mysql_query($sql); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0]; 
$i2=0;


$batas=60;
$menu2 = array();
$alpha2 = array();
$beta2 = array();
$lowalpha2 = array();
$highalpha2 = array();
$lowbeta2 = array();
$highbeta2 = array();

$sql = "SELECT * FROM median1d where sesi='$carisesi' LIMIT $i2,$batas"; 
$rs_result = mysql_query ($sql); 

while( $rows = mysql_fetch_row($rs_result) ){
    

			 $menu2[] = (pow ($rows[2],2));
			 $alpha2[] = (pow ($rows[3],2));
			 $beta2[] = (pow ($rows[4],2));
			 $lowalpha2[] = (pow ($rows[5],2));
			 $highalpha2[] = (pow ($rows[6],2));
			 $lowbeta2[] = (pow ($rows[7],2));
			 $highbeta2[] = (pow ($rows[8],2));
			
			
				}

$sumykuadrat =sum ($menu2);
$sumykuadratalpha =sum ($alpha2);
$sumykuadratbeta =sum ($beta2);
$sumykuadratlowalpha =sum ($lowalpha2);
$sumykuadrathighalpha =sum ($highalpha2);
$sumykuadratlowbeta =sum ($lowbeta2);
$sumykuadrathighbeta =sum ($highbeta2);

$varians1= ($sumykuadrat-((pow($sumy,2))/60))/(60-1);
$variansalpha1= ($sumykuadratalpha-((pow($sumyalpha,2))/60))/(60-1);
$variansbeta1= ($sumykuadratbeta-((pow($sumybeta,2))/60))/(60-1);
$varianslowalpha1= ($sumykuadratlowalpha-((pow($sumylowalpha,2))/60))/(60-1);
$varianshighalpha1= ($sumykuadrathighalpha-((pow($sumyhighalpha,2))/60))/(60-1);
$varianslowbeta1= ($sumykuadratlowbeta-((pow($sumylowbeta,2))/60))/(60-1);
$varianshighbeta1= ($sumykuadrathighbeta-((pow($sumyhighbeta,2))/60))/(60-1);

echo '<br>';




//mencari mean sliding windows x2

$sql = "SELECT COUNT(sesi) FROM median1d where sesi='$carisesi'"; 
$rs_result = mysql_query($sql); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0]; 
for ($i1=60; $i1<=$total_records; $i1++) { 


$batas1=30;
$menu1 = array();
$alpha1 = array();
$beta1 = array();
$lowalpha1 = array();
$highalpha1 = array();
$lowbeta1 = array();
$highbeta1 = array();

$sql = "SELECT * FROM median1d where sesi='$carisesi' LIMIT $i1,$batas1"; 
$rs_result = mysql_query ($sql); 

while( $rows = mysql_fetch_row($rs_result) ){
    

			 $menu1[] = $rows[2];
		     $alpha1[] = $rows[3];
			 $beta1[] = $rows[4];
			 $lowalpha1[] = $rows[5];
			 $highalpha1[] = $rows[6];
			 $lowbeta1[] = $rows[7];
			 $highbeta1[] = $rows[8];
			
				}

//mean1-mean2 sliding windows
echo '<br>';
$mean= average($menu);
$meanalpha= average($alpha);
$meanbeta= average($beta);
$meanlowalpha= average($lowalpha);
$meanhighalpha= average($highalpha);
$meanlowbeta= average($lowbeta);
$meanhighbeta= average($highbeta);
$mean1=average($menu1);
$meanalpha1= average($alpha1);
$meanbeta1= average($beta1);
$meanlowalpha1= average($lowalpha1);
$meanhighalpha1= average($highalpha1);
$meanlowbeta1= average($lowbeta1);
$meanhighbeta1= average($highbeta1);
$selisihmean =$mean-$mean1;
$selisihmeanalpha =$meanalpha-$meanalpha1;
$selisihmeanbeta =$meanbeta-$meanbeta1;
$selisihmeanlowalpha =$meanlowalpha-$meanlowalpha1;
$selisihmeanhighalpha =$meanhighalpha-$meanhighalpha1;
$selisihmeanlowbeta =$meanlowbeta-$meanlowbeta1;
$selisihmeanhighbeta =$meanhighbeta-$meanhighbeta1;


echo '<br>';

// sum varian sliding windows

$sql = "SELECT COUNT(sesi) FROM median1d where sesi='$carisesi'"; 
$rs_result = mysql_query($sql); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0]; 



$batas3=30;
$menu3 = array();
$alpha3 = array();
$beta3 = array();
$lowalpha3 = array();
$highalpha3 = array();
$lowbeta3 = array();
$highbeta3 = array();

$sql = "SELECT * FROM median1d where sesi='$carisesi' LIMIT $i1,$batas3"; 
$rs_result = mysql_query ($sql); 

while( $rows = mysql_fetch_row($rs_result) ){
    

			 $menu3[] = $rows[2];
			 $alpha3[] = $rows[3];
			 $beta3[] = $rows[4];
			 $lowalpha3[] = $rows[5];
			 $highalpha3[] = $rows[6];
			 $lowbeta3[] = $rows[7];
			 $highbeta3[] = $rows[8];
			
				}


echo '<br>';
$sum2= sum($menu3);
$sumalpha2= sum($alpha3);
$sumbeta2= sum($beta3);
$sumlowalpha2= sum($lowalpha3);
$sumhighalpha2= sum($highalpha3);
$sumlowbeta2= sum($lowbeta3);
$sumhighbeta2= sum($highbeta3);



// sum kuadrat varian sliding windows

$sql = "SELECT COUNT(sesi) FROM median1d where sesi='$carisesi'"; 
$rs_result = mysql_query($sql); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0]; 



$batas3=30;
$menu4 = array();
$alpha4 = array();
$beta4 = array();
$lowalpha4 = array();
$highalpha4 = array();
$lowbeta4 = array();
$highbeta4 = array();


$sql = "SELECT * FROM median1d where sesi='$carisesi' LIMIT $i1,$batas3"; 
$rs_result = mysql_query ($sql); 

while( $rows = mysql_fetch_row($rs_result) ){
    

			 $menu4[] = (pow ($rows[2],2));
			 $alpha4[] = (pow ($rows[3],2));
			 $beta4[] = (pow ($rows[4],2));
			 $lowalpha4[] = (pow ($rows[5],2));
			 $highalpha4[] = (pow ($rows[6],2));
			 $lowbeta4[] = (pow ($rows[7],2));
			 $highbeta4[] = (pow ($rows[8],2));
			 
			
			
				}


$sumkuadratslid= sum ($menu4);
$sumkuadratslidalpha= sum ($alpha4);
$sumkuadratslidbeta= sum ($beta4);
$sumkuadratslidlowalpha= sum ($lowalpha4);
$sumkuadratslidhighalpha= sum ($highalpha4);
$sumkuadratslidlowbeta= sum ($lowbeta4);
$sumkuadratslidhighbeta= sum ($highbeta4);

$varians2=($sumkuadratslid-((pow($sum2,2))/30))/(30-1);
$variansalpha2=($sumkuadratslidalpha-((pow($sumalpha2,2))/30))/(30-1);
$variansbeta2=($sumkuadratslidbeta-((pow($sumbeta2,2))/30))/(30-1);
$varianslowalpha2=($sumkuadratslidlowalpha-((pow($sumlowalpha2,2))/30))/(30-1);
$varianshighalpha2=($sumkuadratslidhighalpha-((pow($sumhighalpha2,2))/30))/(30-1);
$varianslowbeta2=($sumkuadratslidlowbeta-((pow($sumlowbeta2,2))/30))/(30-1);
$varianshighbeta2=($sumkuadratslidhighbeta-((pow($sumhighbeta2,2))/30))/(30-1);


$mct=($selisihmean)/(sqrt (((((60*$varians1)+(30*$varians2))/(60+30-2))*((1/60)+(1/30)))));
$mctalpha=($selisihmeanalpha)/(sqrt (((((60*$variansalpha1)+(30*$variansalpha2))/(60+30-2))*((1/60)+(1/30)))));
$mctbeta=($selisihmeanbeta)/(sqrt (((((60*$variansbeta1)+(30*$variansbeta2))/(60+30-2))*((1/60)+(1/30)))));
$mctlowalpha=($selisihmeanlowalpha)/(sqrt (((((60*$varianslowalpha1)+(30*$varianslowalpha2))/(60+30-2))*((1/60)+(1/30)))));
$mcthighalpha=($selisihmeanhighalpha)/(sqrt (((((60*$varianshighalpha1)+(30*$varianshighalpha2))/(60+30-2))*((1/60)+(1/30)))));
$mctlowbeta=($selisihmeanlowbeta)/(sqrt (((((60*$varianslowbeta1)+(30*$varianslowbeta2))/(60+30-2))*((1/60)+(1/30)))));
$mcthighbeta=($selisihmean)/(sqrt (((((60*$varianshighbeta1)+(30*$varianshighbeta2))/(60+30-2))*((1/60)+(1/30)))));
echo $i1."   ".$mct.$mctalpha.$mctbeta.$mctlowalpha.$mcthighalpha.$mctlowbeta.$mcthighbeta;

$sql = "INSERT INTO mct1d1 VALUES (' ','$i1','$mct','$mctalpha','$mctbeta','$mctlowalpha','$mcthighalpha','$mctlowbeta','$mcthighbeta','$carisesi')"; 
$rs_result = mysql_query ($sql); 

}






$array = array(30879,25680.5,22701,18974,18974,18974,18974,18974,18974,18974,35915,51537,56458.5,70413,60037.5,70413,70413,78744,78744,95000,78744,68368.5,54714,54714,55502.5,44285.5,55502.5,55502.5,55502.5,44285.5,32739.5,33324,48279.5,48279.5,47468,56646.5,56646.5,56646.5,62407,87997,87997,99534,115061.5,122809,122809,122809,122809,141296.5,116385.5,107281.5,107281.5,115254.5,115254.5,82758.5,80008,80008,59045,51773,51773,45879.5);
//echo average($array); // 10






/*$sql = "SELECT COUNT(sesi) FROM gelombang where sesi='$carisesi'"; 
$rs_result = mysql_query($sql); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0]; 
for ($i=0; $i<=$total_records; $i++) { 


$batas=10;
$menu = array();
$menu1 = array();
$menu2 = array();
$menu3 = array();
$menu4 = array();
$menu5 = array();
$menu6 = array();
$sql = "SELECT * FROM gelombang where sesi='$carisesi' LIMIT $i,$batas"; 
$rs_result = mysql_query ($sql); 

while( $rows = mysql_fetch_row($rs_result) ){
    
			 $menu[] = $rows[5];
			$menu1[] = $rows[6];
			$menu2[] = $rows[7];
			$menu3[] = $rows[8];
			$menu4[] = $rows[9];
			$menu5[] = $rows[10];
			$menu6[] = $rows[11];
			
				
			

			
    
    ?>
	
		  
	
  
  
	<tbody>
          <tr>
            <td>
<?php


}

$theta = calculate_median($menu);
$alpha = calculate_median($menu1);
$beta = calculate_median($menu2);
$low_alpha = calculate_median($menu3);
$high_alpha = calculate_median($menu4);
$low_beta = calculate_median($menu5);
$high_beta = calculate_median($menu6);
 
 


$sql = "INSERT INTO median
VALUES (' ','$i','$theta','$alpha','$beta','$low_alpha','$high_alpha','$low_beta','$high_beta','$carisesi')"; 
$rs_result = mysql_query ($sql); 
}


*/

?>
</td>
          </tr>
            
         
		  
		  </table>






